  set more off
  set seed 19112014

*===================================================================*
*   BIHAR EVALUATION OF SOCIAL FRANCHISING AND TELEMEDICINE (BEST)
*                   Processing SP data - Follow Up
*====================================================================*


*====================================================================*
* DIARRHEA
*====================================================================*

* Open database
  use "$prodata2\standardized_patient_diarrhea_a", clear

* Sample Selection II
/*exclude doctor that only asked the father to bring the child
  c1 did doctor ask to bring the child
  c2 treatment not given(1)/given(2) */
  egen qmiss=rowmean(h1a-h24b)
  replace sample2 = 3 if c1==1 & c2==1 & qmiss==2
  replace sample2 = 4 if c1==2 & c2==1 & qmiss==2
  lab def sample2 1 "complete" 2 "incorrect statement" 3 "child + nothing" 4 "neither child nor treatment"
  lab val sample2 sample2
  drop qmiss


***********************************************
* General Questions
***********************************************

* Source
  gen     source = "SP Second Round"
  lab var source "Data source"

* Wave
  gen     wave = 2
  lab var wave "Wave"

* WHP-TPC Providers - OVERSAMPLED
  gen UID = prov_id
  merge 1:1 UID using "$rawdatap2\DeidentifiedProviderListingData_270315", keepus(Provider_Type)
  drop if _merge==2

  gen     TPC = 0 if Provider_Type!=.
  replace TPC = 1 if Provider_Type==3
  lab var TPC "WHP-TPC provider"
  tab TPC, nol
  drop _merge UID Provider_Type

* WHP-RHP Providers - NON-OVERSAMPLED
  gen uid = prov_id
  merge 1:1 uid using "$rawdatap2\specific_whp_providers_sample", keepus(sky_care_id_1) force
  drop if _merge==2

  gen     RHP = 0 if TPC!=. & _merge!=3
  replace RHP = 1 if _merge==3
  lab var RHP "WHP non-oversampled"
  drop _merge sky_care_id_1

* Fixing TPC identification and identifying oversampled
  list prov_id TPC RHP
  list prov_id TPC RHP if TPC==1 | RHP==1
  replace TPC = 0 if TPC==. & RHP==1
  replace TPC = 0 if TPC==1 & RHP==1

  merge 1:1 uid using "$rawdatap2\tpc_providers"
  drop if _merge==2
  replace TPC = 1 if _merge==3

  gen     WHP_oversampled = 0 
  replace WHP_oversampled = 1 if _merge==3
  lab var WHP_oversampled "=1 if oversampled WHP"
  drop _merge cluster_code- village_code

* WHP Providers - ALL
  gen     WHP = 0 if TPC!=.
  replace WHP = 1 if TPC==1 | RHP==1
  lab var WHP "World Health Partners (WHP) provider"

* Date SP interaction
  gen     sp_date=date(qq10,"DMY")
  format  sp_date %d
  lab var sp_date "Date of SP-Provider Interaction"

* SP id
  gen     spp_id = qq11a 
  lab var spp_id "Standarized Patient id"
  
* Interviewer id
  gen     intv_id = qq12a
  lab var intv_id "Interviewer id"

* Day of week for visit
  gen day =day(sp_date)
  gen month=month(sp_date)
  gen year=year(sp_date)
  gen dow = dow(mdy(month, day,year))
  lab var dow "Day of week of SP interaction"
  lab def week 0 "Sunday" 1 "Monday" 2 "Tuesday" 3 "Wednesday" 4 "Thursday" 5 "Friday" 6 "Saturday"
  lab val dow week
  drop day month year

* Hour of visit
  gen double ctime =clock(qq8, "hm")
  format ctime %tcHH:MM
  gen hour=hh(ctime)
  gen minute = mm(ctime)
  lab var ctime  "Consultation Time"
  lab var hour   "Hour of SP interaction"
  lab var minute "Minutes of SP interaction"

* Visit Length (minutes)
  split qq9, p(:)
  destring qq91 qq92 qq93, replace
  gen     visit_time = qq91*60 + qq92 + qq93/60
  format  visit_time %9.2f
  lab var visit_time "visit length (minutes)"
  gen double vtime =clock(qq9, "hms")
  format vtime %tcMM:SS
  cap drop qq9* 

* Patient Caseload
  gen     pat_before=n1
  lab var pat_before "No. of patient before visit"
  gen     pat_after=n2
  lab var pat_after "No. of patient after visit"
  egen    caseload=rsum(pat_before pat_after)
  lab var caseload "patient caseload"

* Doctor ask to bring the child
  recode c1 (2=0), gen (ask_child)
  lab var ask_child "=1 if doctor asked to bring the child"

***********************************************
* Medical History
***********************************************

* Child Age
/*NOTE IMPORTANT: child age was the first question in the baseline, so I am going to 
  change the numbers of the questions one place to have h1a as child age.*/

  forvalues n=26(-1)1 {
    local m=`n'+1
    rename h`n'a h`m'a
    rename h`n'b h`m'b
    }

  gen     h1a = 0
  replace h1a = 1 if regexm(h27_1, "CHILD'S AGE")
  replace h1a = 1 if regexm(h27_2, "CHILD'S AGE")
  replace h1a = 1 if regexm(h27_3, "CHILD'S AGE")
  lab var h1a "Child' age"
  order h1a, before(h2a)

* Recoding variables (recode 2 to be 0)
  foreach v of varlist h2a-h27b {
    replace `v'=0 if `v'==2
      }

* Recoding values according to extra questions
  replace h26a=1 if prov_id==341026  //any medicine given?
  replace h17a=1 if prov_id==225023  //breastfeeding
  replace h17a=1 if prov_id==265017  //breastfeeding
  replace h17a=1 if prov_id==103028  //breastfeeding
  replace h22a=1 if prov_id==348044 //taking fluid
  replace h26a=1 if prov_id==445051  //any medicine given?

* Reorder variables
  order h2b, after(h27a)
  forvalues n=3/27 {
    local nn=`n'-1
    order h`n'b, after(h`nn'b)
      }

* Number of Medical History questions asked
  egen    nhistory = rowtotal(h1a-h22a h26a h27a), missing
  lab var nhistory "No. of Medical History questions asked"

* Essential question for cause of disease
  egen    qcause=rsum(h2a h3a h4a h5a h11a h12a h14a h15a h18a h19a h20a)
  lab var qcause "No. of ques. asked related to cause of disease"

* Essential question for severity of desease
  egen    qseverity = rsum(h6a h7a h8a h9a h10a h21a)
  lab var qseverity "No. of ques. asked related to severity of disease"

* Number of essential items obtained by providers in history-taking
  egen    nessential=rsum(qcause qseverity)
  lab var nessential "No. of essential items obtained in history-taking"

* Percentage of essential items obtained by providers in history-taking
  gen     pessential = nessential
  replace pessential = pessential/17
  lab var pessential "% of essential items obtained in history-taking"

***********************************************
* Social History
***********************************************

* Social history taking
  egen    qsocial=rsum(h23a h24a h25a)
  replace qsocial=1 if qsocial>1 & qsocial!=.
  lab var qsocial "=1 if provider asked about social history"


***********************************************
* Treatment
***********************************************

* Any Treatment
/*c2 treatment not given(1)/given(2) */
  recode c2 (1=0) (2=1), gen(tx_any)
  egen aux=rownonmiss(t1_?a), strok
  replace tx_any=1 if (aux>0 & aux!=.)
  lab var tx_any "=1 if provider gave treatment"
  drop aux

* Medicines dispensed/prescribed
/*NOTE: now t1_? if 1 for dispensed medicines and 2 for prescribed*/
  gen     med_dispensed = 0
  lab var med_dispensed "medicines dispensed"
  gen     med_prescribed = 0
  lab var med_prescribed "medicines prescribed"
  forvalues n=1/6 {
    replace med_dispensed  = 1 if t1_`n'==1
    replace med_prescribed = 1 if t1_`n'==2
    }


***********************************************
* Correct Treatment
***********************************************

* Merge
  sort case prov_id
  merge 1:1 case prov_id using "$rawdatap2\correct_treatment_sp.dta"
  keep if _merge==3
  drop _merge tx_detail3

* Rename
  lab var tx_detail1 "detailed treatment"
  lab var nmedicines "# of medicines prescribed"



***********************************************
* Recomendations
***********************************************

* Counselling on hygiene especially washing hands
  gen     counselling = (t5==1) & t5!=.
  replace counselling =1 if t9==1
  lab var counselling "=1 if counselling on hygiene"

* Counselling on how to feed the child
  gen     counselling_food = (t6==1) if t6!=.
  replace counselling_food =1 if t12==1
  lab var counselling_food "=1 if counselling on child food"

* Explanation about the medicines.
  gen     explanation = (t2==1) & t2!=.
  lab var explanation "=1 if provides explanation about medicines"

* Explanation given by whom?
  gen     expl_who = t4
  lab var expl_who "who gave the explanation"
  lab def expl 1 "doctor" 2 "Compounder" 3 "nurse"
  lab val expl_who expl 

* Number of questions if treatment/no treatment
  gen     qs_treat = nhistory if tx_any==1
  lab var qs_treat "number of questions asked when treatment given"

  gen     qs_notreat = nhistory if tx_any==0
  lab var qs_notreat "number of questions asked when no treatment given"

* Referral
  gen     referral = (t13==1) if t13!=.
  lab var referral "=a if referral recomendation"

***********************************************
* Diagnosis
***********************************************

* Recode
  foreach v of varlist d1 d3 {
  recode `v' (2=0)
    }

* Any Diagnosis
  gen     dx_any = d1
  lab var dx_any "=1 if any kind of diagnosis was given"

* Diagnosis
  gen     dx = d2
  lab var dx "Diagnosis"

* Type of dignosis given
  gen     dx_type = 0
  replace dx_type = 1 if d2=="DIARRHEA"
  lab var dx_type "=1 if diagnosis was corrrect, when done"
  tab dx_type

***********************************************
* Fees
***********************************************

* Fee
  gen     fee = f1
  lab var fee "fee charged by practitioner"

  gen     fee_med =f2
  lab var fee_med "cost of medicines paid in addition"

  gen     fee_total=f3
  lab var fee_total "total cost"

***********************************************
* Follow up questions
***********************************************

  gen     ask_return=(r1==1) & r1!=.
  lab var ask_return "=1 if doctor ask the patient to return"

* When did he/she asked you to return
  forvalues n=1/3 {
  local m=`n'+1
  gen return_`n'=(r`m'==1) if ask_return==1
    }
  gen     return_4 =(r5a!=.|r5b!=.) if ask_return==1
  replace return_4 = 0 if r5a==0 & r5b==0

  lab var return_1 "=1 return if not feel better"
  lab var return_2 "=1 return to get a refill of medicine"
  lab var return_3 "=1 return after completition of test"
  lab var return_4 "=1 return after days/weeks"

***********************************************
* SP Doctor's analysis
***********************************************
  gen     like_doctor=(g1==1) & g1!=.
  lab var like_doctor "=1 if SP liked the doctor"

  gen     again_doctor=(g2==1) & g2!=.
  lab var again_doctor "=1 if patient back to doctor"

  gen     ask_other=(h27==1)
  lab var ask_other "=1 if providers asked other questions"

***********************************************
* GLOBAL ASSESSMENT SCALE
***********************************************
  egen    gas = rmean(g3-g7)
  lab var gas "global assesment scale - max. 15"

***********************************************
* ITR
***********************************************
/*Selected items
  For Diarrhea case
  1: h1a   age of child
  2: h2a   qualities of stool
  3: h3a   frequency
  4: h4a   quantity of stool
  5: h5a   urination
  6: h6a   active/playful
  7: h7a   fever
  8: h8a   abdominal pain
  9: h9a   vomiting
  10: h13a what has the child eaten
  11: h14a taking fluids
  12: t3   Counseling on hygiene specially washing hands (diarrhea) */

* Selected items should begin with the same string
  gen  sp_item1 = h1a
  egen sp_item2 = anymatch(h2a h3a h4a h5a), v(1)
  egen miss=rowmiss(h2a h3a h4a h5a)
  replace sp_item2 =. if miss==4
  drop miss
  gen  sp_item3 = h6a
  gen  sp_item4 = h7a
  egen sp_item5 = anymatch(h8a h9a), v(1)
  egen miss=rowmiss(h8a h9a)
  replace sp_item5 =. if miss==2
  drop miss
  gen  sp_item6 = h10a
  egen sp_item7 = anymatch(h11a h12a), v(1)
  egen miss=rowmiss(h11a h12a)
  replace sp_item7 =. if miss==2
  drop miss
  gen  sp_item8 = h14a
  gen  sp_item9 = h15a
  gen  sp_item10 = h20a
  gen  sp_item11 = h22a
  gen  sp_item12 = counselling

  forvalues i=1/12 {
    lab var sp_item`i' "IRT: Item `i'"
    }

  order sp_item*, seq last

* Estimate theta for providers who treated cases of diarrhea
  preserve
  keep if sample2==1
  keep prov_id sp_item1-sp_item12
  openirt, id(prov_id) item_prefix(sp_item) save_item_parameters("$irtdata2\sp_items_parameters_1.dta") save_trait_parameters("$irtdata2\sp_traits_1.dta")
  restore

* Merge data with Theta Score
  gen id=prov_id
  merge 1:1 id using "$irtdata2\sp_traits_1.dta"
  renpfix theta sp_theta
  drop _merge
  drop id

* Standardizing thetas
  egen    sp_theta_eap_std = std(sp_theta_eap)
  lab var sp_theta_eap_std "Standardized Theta (EAP)"
  egen    sp_theta_mle_std = std(sp_theta_mle)
  lab var sp_theta_mle_std "Standardized Theta (MLE)"
  drop sp_theta_pv1-sp_theta_pv5 sp_theta_mle_se


***********************************************
* Know-do Gap
* 12 Main questions
***********************************************
  egen sp_dq1 = anymatch(h2a h3a h4a h5a), v(1)
  egen miss=rowmiss(h2a h3a h4a h5a)
  replace sp_dq1 =. if miss==4
  drop miss
  gen  sp_dq2 = h6a
  gen  sp_dq3 = h7a
  egen sp_dq4 = anymatch(h8a h9a), v(1)
  egen miss=rowmiss(h8a h9a)
  replace sp_dq4 =. if miss==2
  drop miss
  gen  sp_dq5 = h10a
  egen sp_dq6 = anymatch(h11a h12a), v(1)
  egen miss=rowmiss(h11a h12a)
  replace sp_dq6 =. if miss==2
  drop miss
  gen  sp_dq7 = h14a
  gen  sp_dq8 = h15a
  egen sp_dq9 = anymatch(h16a h17a), v(1)
  egen miss=rowmiss(h16a h17a)
  replace sp_dq9 =. if miss==2
  drop miss
  gen  sp_dq10 = h20a
  gen  sp_dq11 = h22a
  egen sp_dq12 = anymatch(h23a h24a h25a), v(1)
  egen miss=rowmiss(h23a h24a h25a)
  replace sp_dq12 =. if miss==3
  drop miss

  lab var sp_dq1 "Diarrhea: Qualities of stool"
  lab var sp_dq2 "Diarrhea: Frequency"
  lab var sp_dq3 "Diarrhea: Quantity of stool"
  lab var sp_dq4 "Diarrhea: Questions about urination"
  lab var sp_dq5 "Diarrhea: Is the child active/playful?"
  lab var sp_dq6 "Diarrhea: Fever"
  lab var sp_dq7 "Diarrhea: Abdominal pain"
  lab var sp_dq8 "Diarrhea: Vomiting"
  lab var sp_dq9 "Diarrhea: Source of drinking water"
  lab var sp_dq10 "Diarrhea: What has the child eaten?"
  lab var sp_dq11 "Diarrhea: Taking fluids"
  lab var sp_dq12 "Diarrhea: Physical Environment"

* IRT Performance based on 12 questions

* Estimate theta for providers who treated cases of diarrhea
  preserve
  keep if sample2==1
  keep prov_id sp_dq1-sp_dq12
  openirt, id(prov_id) item_prefix(sp_dq) save_item_parameters("$irtdata2\sp_items_parameters_1_q12.dta") save_trait_parameters("$irtdata2\sp_traits_1_q12.dta")
  restore

* Merge data with Theta Score based on 12 questions
  gen id=prov_id
  merge 1:1 id using "$irtdata2\sp_traits_1_q12.dta", keepus(theta_eap theta_mle) 
  rename theta_eap sp_theta_eap_q12
  rename theta_mle sp_theta_mle_q12
  drop _merge
  drop id

* Standardizing thetas
  egen    sp_theta_eap_q12_std = std(sp_theta_eap_q12)
  lab var sp_theta_eap_q12_std "Standardized Theta (EAP)"
  egen    sp_theta_mle_q12_std = std(sp_theta_mle_q12)
  lab var sp_theta_mle_q12_std "Standardized Theta (MLE)"

* Estimate theta for providers who treated cases of diarrhea and are not oversampled WHP
  preserve
  keep if sample2==1 & WHP_oversampled==0
  keep prov_id sp_dq1-sp_dq12
  openirt, id(prov_id) item_prefix(sp_dq) save_item_parameters("$irtdata2\sp_items_parameters_1_q12b.dta") save_trait_parameters("$irtdata2\sp_traits_1_q12b.dta")
  restore

* Merge data with Theta Score based on 12 questions
  gen id=prov_id
  merge 1:1 id using "$irtdata2\sp_traits_1_q12b.dta", keepus(theta_eap theta_mle) 
  rename theta_eap sp_theta_eap_q12b
  rename theta_mle sp_theta_mle_q12b
  drop _merge
  drop id

* Standardizing thetas
  egen    sp_theta_eap_q12_stdb = std(sp_theta_eap_q12b)
  lab var sp_theta_eap_q12_stdb "Standardized Theta (EAP)"
  egen    sp_theta_mle_q12_stdb = std(sp_theta_mle_q12b)
  lab var sp_theta_mle_q12_stdb "Standardized Theta (MLE)"

***********************************************
* Correcting District code to match with Baseline
***********************************************
  rename district district_or
  recode district_or (2=3) (3=5) (4=7) (5=4) (6=10) (7=8) (8=6) (9=11) (10=2) (11=9), gen(district)
  order district, after(district_or)

***********************************************
* Changes in cluster 387
***********************************************

  replace block = 134 if block==142 & cluster==387
  replace cluster = 462 if cluster ==387

***********************************************
* Saving processed data
***********************************************
  gen last=.
  order prov_id, after(qq0)
  keep case sample2 prov_id-cluster source-last
  order source, first
  drop last
  sort prov_id
  label data "Standardized Patient Diarrhea Second Round"
  compress
  save "$prodata2\standardized_patient_diarrhea_b", replace





*====================================================================*
* PNEUMONIA
*====================================================================*

* Open database
  use "$prodata2\standardized_patient_pneumonia_a", clear

* Sample Selection II
/*exclude doctor that only asked the father to bring the child
  c1 did doctor ask to bring the child
  c2 treatment not given(1)/given(2) */
  egen qmiss=rowmean(h1a-h24b)
  replace sample3 = 3 if c1==1 & c2==1 & qmiss==2
  replace sample3 = 4 if c1==2 & c2==1 & qmiss==2
  lab def sample3 1 "complete" 2 "incorrect statement" 3 "child + nothing" 4 "neither child nor treatment"
  lab val sample3 sample3
  drop qmiss
  
***********************************************
* General Questions
***********************************************

* Source
  gen     source = "SP Second Round"
  lab var source "Data source"

* Wave
  gen     wave = 2
  lab var wave "Wave"

* WHP-TPC Providers - OVERSAMPLED
  gen UID = prov_id
  merge 1:1 UID using "$rawdatap2\DeidentifiedProviderListingData_270315", keepus(Provider_Type)
  drop if _merge==2

  gen     TPC = 0 if Provider_Type!=.
  replace TPC = 1 if Provider_Type==3
  lab var TPC "WHP-TPC provider"
  tab TPC, nol
  drop _merge UID Provider_Type

* WHP-RHP Providers - NON-OVERSAMPLED
  gen uid = prov_id
  merge 1:1 uid using "$rawdatap2\specific_whp_providers_sample", keepus(sky_care_id_1) force
  drop if _merge==2

  gen     RHP = 0 if TPC!=. & _merge!=3
  replace RHP = 1 if _merge==3
  lab var RHP "WHP non-oversampled"
  drop _merge sky_care_id_1

* Fixing TPC identification and identifying oversampled
  list prov_id TPC RHP
  list prov_id TPC RHP if TPC==1 | RHP==1
  replace TPC = 0 if TPC==. & RHP==1
  replace TPC = 0 if TPC==1 & RHP==1

  merge 1:1 uid using "$rawdatap2\tpc_providers"
  drop if _merge==2
  replace TPC = 1 if _merge==3

  gen     WHP_oversampled = 0 
  replace WHP_oversampled = 1 if _merge==3
  lab var WHP_oversampled "=1 if oversampled WHP"
  drop _merge cluster_code- village_code

* WHP Providers - ALL
  gen     WHP = 0 if TPC!=.
  replace WHP = 1 if TPC==1 | RHP==1
  lab var WHP "World Health Partners (WHP) provider"

* Date SP interaction
  gen     sp_date=date(qq10,"DMY")
  format  sp_date %d
  lab var sp_date "Date of SP-Provider Interaction"

* SP id
  gen     spp_id = qq11a 
  lab var spp_id "Standarized Patient id"
  
* Interviewer id
  gen     intv_id = qq12a
  lab var intv_id "Interviewer id"

* Day of week for visit
  gen day =day(sp_date)
  gen month=month(sp_date)
  gen year=year(sp_date)
  gen dow = dow(mdy(month, day,year))
  lab var dow "Day of week of SP interaction"
  lab def week 0 "Sunday" 1 "Monday" 2 "Tuesday" 3 "Wednesday" 4 "Thursday" 5 "Friday" 6 "Saturday"
  lab val dow week
  drop day month year

* Hour of visit
  gen double ctime =clock(qq8, "hm")
  format ctime %tcHH:MM
  gen hour=hh(ctime)
  gen minute = mm(ctime)
  lab var ctime  "Consultation Time"
  lab var hour   "Hour of SP interaction"
  lab var minute "Minutes of SP interaction"

* Visit Length (minutes)
  split qq9, p(:)
  destring qq91 qq92 qq93, replace
  gen     visit_time = qq91*60 + qq92 + qq93/60
  format  visit_time %9.2f
  lab var visit_time "visit length (minutes)"
  gen double vtime =clock(qq9, "hms")
  format vtime %tcMM:SS
  cap drop qq9* 

* Patient Caseload
/*n1: How many patients were waiting when you reached the clinic?
  n2: How many patients were in the clinic when you left? */
  gen     pat_before=n1
  lab var pat_before "No. of patient before visit"
  gen     pat_after=n2
  lab var pat_after "No. of patient after visit"
  egen    caseload=rsum(pat_before pat_after)
  lab var caseload "patient caseload"

* Doctor ask to bring the child
/*c1: Did doctor ask to bring the child?*/
  recode c1 (2=0), gen (ask_child)
  lab var ask_child "=1 if doctor asked to bring the child"


***********************************************
* Medical History
***********************************************

* Child Age
/*NOTE IMPORTANT: child age was the first question in the baseline, so I am going to 
  change the numbers of the questions one place to have h1a as child age.*/

  forvalues n=29(-1)1 {
    local m=`n'+1
    rename h`n'a h`m'a
    rename h`n'b h`m'b
    }

  gen     h1a = 0
  replace h1a = 1 if regexm(h30_1, "CHILD'S AGE")
  replace h1a = 1 if regexm(h30_2, "CHILD'S AGE")
  replace h1a = 1 if regexm(h30_3, "CHILD'S AGE")
  lab var h1a "Child' age"
  order h1a, before(h2a)

* Recoding variables
  foreach v of varlist h1a-h30b {
    replace `v'=0 if `v'==2
      }

* Recoding values according to extra questions
/*ANY PHELGM IN THE VOMITING
  DID THE CHILD GET DRENCHED?
  DOES YOUR CHILD HAVE GAS?
  DOES YOUR CHILD HAVE LOOSE MOTION?
  DOES YOUR CHILD HAVE STOMACHACHE?
  HAS YOUR CHILD HAD PNEUMONIA IN THE PAST?
  HE HAD PNEUMONIA BEFORE ALSO?                  // only this one
  HOW MUCH WATER IS THE CHILD DRINKING?
  IS THE CHILD IS PASSING STOOL HOW MANY TIMES?
  IS THE CHILD STOMACH BLOTED?
  IS THE CHILD THIRSTY?
  IS THE CHILD VOMITING?
  IS YOUR CHILD STOMACH UPSET?
  MUCUS IN THE STOOL?
  NATURE OF STOOL
  WHAT IS CHILD'S URINE COLOUR?*/

  replace h21a=1 if prov_id==200017
  replace h21a=1 if prov_id==341032
  replace h21a=1 if prov_id==87049
  replace h21a=1 if prov_id==273007

* Urination/tears
  gen h31a = 0 
  forvalues n=1/3 {
    replace h31a = 1 if regexm(h30_`n',"WATER")
    replace h31a = 1 if regexm(h30_`n',"THIRSTY")
    replace h31a = 1 if regexm(h30_`n',"URINE")
    }
  lab var h31a "Urination/deshidratation"

* Reorder variables
  order h2b, after(h30a)
  forvalues n=3/30 {
  local nn=`n'-1
  order h`n'b, after(h`nn'b)
    }
  order h31a, after(h30a)

* Number of Medical History questions asked
  egen    nhistory = rowtotal(h1a-h30a), missing
  lab var nhistory "No. of Medical History questions asked"

* Essential question for cause of desease
  egen    qcause=rsum(h2a h10a-h13a)
  lab var qcause "No. of ques. asked related to cause of disease"

* Essential question for severity of desease
  egen    qseverity = rsum(h16a h6a h7a h31a)
  lab var qseverity "No. of ques. asked related to severity of disease"

* Number of essential items obtained by providers in history-taking
  egen    nessential=rsum(qcause qseverity)
  lab var nessential "No. of essential items obtained in history-taking"

* Percentage of essential items obtained by providers in history-taking
  gen     pessential = nessential
  replace pessential = pessential/9
  lab var pessential "% of essential items obtained in history-taking"

***********************************************
* Social History
***********************************************

* Social history taking
  egen    qsocial=rsum(h17a h28a)
  replace qsocial=1 if qsocial>1 & qsocial!=.
  lab var qsocial "=1 if provider asked about social history"

***********************************************
* Treatment
***********************************************

* Any Treatment
  recode c2 (1=0) (2=1), gen(tx_any)
  egen aux=rownonmiss(t1_?a), strok
  replace tx_any=1 if (aux>0 & aux!=.)
  lab var tx_any "=1 if provider gave treatment"
  drop aux

* Medicines dispensed/prescribed
  gen     med_dispensed = 0
  lab var med_dispensed "medicines dispensed"
  gen     med_prescribed = 0
  lab var med_prescribed "medicines prescribed"
  forvalues n=1/6 {
    replace med_dispensed  = 1 if t1_`n'==1
    replace med_prescribed = 1 if t1_`n'==2
    }


***********************************************
* Correct Treatment
***********************************************

* Merge
  sort case prov_id
  merge 1:1 case prov_id using "$rawdatap2\correct_treatment_sp.dta"
  keep if _merge==3
  drop _merge tx_detail1

* Rename
  lab var tx_detail3 "detailed treatment"
  lab var nmedicines "# of medicines prescribed"

* Counselling on food
  gen     counselling_food = (t8==1) if t8!=.
  lab var counselling_food "=1 if counselling on dietary"

* Explanation about the medicines.
  gen     explanation = (t2==1) & t2!=.
  lab var explanation "=1 if provides explanation about medicines"

* Explanation given by whom?
/*t5: Explanation given by whom? 1=Doctor; 2=Compounder; 3=Nurse*/
  gen     expl_who = t4
  lab var expl_who "who gave the explanation"
  lab def expl 1 "doctor" 2 "Compounder" 3 "nurse"
  lab val expl_who expl 

* Number of questions if treatment/no treatment
  gen     qs_treat = nhistory if tx_any==1
  lab var qs_treat "number of questions asked when treatment given"

  gen     qs_notreat = nhistory if tx_any==0
  lab var qs_notreat "number of questions asked when no treatment given"

* Referral
  gen     referral = (t5==1) if t5!=.
  lab var referral "=a if referral recomendation"


***********************************************
* Diagnosis
***********************************************

* Recode
  foreach v of varlist d1 d3 {
  recode `v' (2=0)
    }

* Any Diagnosis
  gen     dx_any = d1
  lab var dx_any "=1 if any kind of diagnosis was given"

* Diagnosis
  gen     dx = d2
  lab var dx "Diagnosis"

* Type of dignosis given
  gen code=soundex(d2)
  gen     dx_type = 0
  replace dx_type = 1 if regexm(d2,"PNEUMONIA")
  lab var dx_type "=1 if diagnosis was corrrect, when done"
  drop code

***********************************************
* Fees
***********************************************

* Fee
  gen     fee = f1
  lab var fee "fee charged by practitioner"

  gen     fee_med =f2
  lab var fee_med "cost of medicines paid in addition"

  gen     fee_total=f3
  lab var fee_total "total cost"

***********************************************
* Follow up questions
***********************************************
  gen     ask_return=(r1==1)
  lab var ask_return "=1 if doctor ask the patient to return"

* When did he/she asked you to return
  forvalues n=1/3 {
  local m=`n'+1
  gen return_`n'=(r`m'==1) if ask_return==1
    }
  gen     return_4 =(r5a!=.|r5b!=.) if ask_return==1
  replace return_4 = 0 if r5a==0 & r5b==0

  lab var return_1 "=1 return if not feel better"
  lab var return_2 "=1 return to get a refill of medicine"
  lab var return_3 "=1 return after completition of test"
  lab var return_4 "=1 return after days/weeks"

***********************************************
* SSP Doctor's analysis
***********************************************

  gen     like_doctor=(g1==1) & g1!=.
  lab var like_doctor "=1 if SP liked the doctor"

  gen     again_doctor=(g2==1) & g2!=.
  lab var again_doctor "=1 if patient back to doctor"

  gen     ask_other=(h30==1)
  lab var ask_other "=1 if providers asked other questions"


***********************************************
* GLOBAL ASSESSMENT SCALE
***********************************************
  egen    gas = rmean(g3-g7)
  lab var gas "global assesment scale - max. 15"

***********************************************
* ITR
***********************************************

* Selected items should begin with the same string
  
  gen  sp_item1 = h1a
  gen  sp_item2 = h2a
  gen  sp_item3 = h10a
  egen sp_item4 = anymatch(h11a h12a h13a), v(1)
  egen miss=rowmiss(h11a h12a h13a)
  replace sp_item4 =. if miss==3
  drop miss
  gen  sp_item5 = h3a
  gen  sp_item6 = h5a
  gen  sp_item7 = h15a
  egen sp_item8 = anymatch(h6a h7a), v(1)
  egen miss=rowmiss(h6a h7a)
  replace sp_item8 =. if miss==2
  drop miss
  egen sp_item9 = anymatch(h23a h25a), v(1)
  egen miss=rowmiss(h23a h25a)
  replace sp_item9 =. if miss==2
  drop miss
  gen  sp_item10 = h17a

  forvalues i=1/10 {
    lab var sp_item`i' "IRT: Item `i'"
    }

  order sp_item*, seq last

* Estimate theta for providers who treated cases of pneumonia
  preserve
  keep if sample3==1
  keep prov_id sp_item1-sp_item10
  openirt, id(prov_id) item_prefix(sp_item) save_item_parameters("$irtdata2\sp_items_parameters_2.dta") save_trait_parameters("$irtdata2\sp_traits_2.dta")
  restore

* Merge data with Theta Score
  gen id=prov_id
  merge 1:1 id using "$irtdata2\sp_traits_2.dta"
  renpfix theta sp_theta
  drop _merge
  drop id

* Standardizing thetas
  egen    sp_theta_eap_std = std(sp_theta_eap)
  lab var sp_theta_eap_std "Standardized Theta (EAP)"
  egen    sp_theta_mle_std = std(sp_theta_mle)
  lab var sp_theta_mle_std "Standardized Theta (MLE)"
  
  drop sp_theta_pv1-sp_theta_pv5 sp_theta_mle_se
  

***********************************************
* Know-do Gap
* 12 Main questions
***********************************************

  gen  sp_pq1 = h2a
  gen  sp_pq2 = h10a
  egen sp_pq3 = anymatch(h11a h12a h13a), v(1)
  egen miss=rowmiss(h11a h12a h13a)
  replace sp_pq3 =. if miss==3
  drop miss
  gen  sp_pq4 = h3a
  gen  sp_pq5 = h5a
  gen  sp_pq6 = h15a
  gen  sp_pq7 = h16a
  egen sp_pq8 = anymatch(h6a h7a), v(1)
  egen miss=rowmiss(h6a h7a)
  replace sp_pq8 =. if miss==2
  drop miss
  egen sp_pq9 = anymatch(h23a h25a), v(1)
  egen miss=rowmiss(h23a h25a)
  replace sp_pq9 =. if miss==2
  drop miss
  egen sp_pq10 = anymatch(h18a h19a h20a h21a), v(1)
  egen miss=rowmiss(h18a h19a h20a h21a)
  replace sp_pq10 =. if miss==4
  drop miss
  gen  sp_pq11 = h28a
  gen  sp_pq12 = h17a

  lab var sp_pq1 "Pneumonia: Fever"
  lab var sp_pq2 "Pneumonia: Breathing is rapid"
  lab var sp_pq3 "Pneumonia: Difficulty in breathing/nostrils flaring/skin between ribs moves inwards/neck muscles are strained"
  lab var sp_pq4 "Pneumonia: Type of cough"
  lab var sp_pq5 "Pneumonia: Runny nose or nose blocked"
  lab var sp_pq6 "Pneumonia: Sounds while breathing"
  lab var sp_pq7 "Pneumonia: Change in colour of fingernails, hand, toes"
  lab var sp_pq8 "Pneumonia: Child is weak"
  lab var sp_pq9 "Pneumonia: Breastfeeding/immunization history"
  lab var sp_pq10 "Pneumonia: History of asthma/alleries"
  lab var sp_pq11 "Pneumonia: Have a chula at home"
  lab var sp_pq12 "Pneumonia: Other children have similar fever/cough"

* IRT Performance based on 12 questions

* Estimate theta for providers who treated cases of pneumonia
  preserve
  keep if sample3==1
  keep prov_id sp_pq1-sp_pq12
  openirt, id(prov_id) item_prefix(sp_pq) save_item_parameters("$irtdata2\sp_items_parameters_2_q12.dta") save_trait_parameters("$irtdata2\sp_traits_2_q12.dta")
  restore

* Merge data with Theta Score based on 12 questions
  gen id=prov_id
  merge 1:1 id using "$irtdata2\sp_traits_2_q12.dta", keepus(theta_eap theta_mle) 
  rename theta_eap sp_theta_eap_q12
  rename theta_mle sp_theta_mle_q12
  drop _merge
  drop id

* Standardizing thetas
  egen    sp_theta_eap_q12_std = std(sp_theta_eap_q12)
  lab var sp_theta_eap_q12_std "Standardized Theta (EAP)"
  egen    sp_theta_mle_q12_std = std(sp_theta_mle_q12)
  lab var sp_theta_mle_q12_std "Standardized Theta (MLE)"

* Estimate theta for providers who treated cases of pneumonia and are not oversampled WHP
  preserve
  keep if sample3==1 & WHP_oversampled==0
  keep prov_id sp_pq1-sp_pq12
  openirt, id(prov_id) item_prefix(sp_pq) save_item_parameters("$irtdata2\sp_items_parameters_2_q12b.dta") save_trait_parameters("$irtdata2\sp_traits_2_q12b.dta")
  restore

* Merge data with Theta Score based on 12 questions
  gen id=prov_id
  merge 1:1 id using "$irtdata2\sp_traits_2_q12b.dta", keepus(theta_eap theta_mle) 
  rename theta_eap sp_theta_eap_q12b
  rename theta_mle sp_theta_mle_q12b
  drop _merge
  drop id

* Standardizing thetas
  egen    sp_theta_eap_q12_stdb = std(sp_theta_eap_q12b)
  lab var sp_theta_eap_q12_stdb "Standardized Theta (EAP)"
  egen    sp_theta_mle_q12_stdb = std(sp_theta_mle_q12b)
  lab var sp_theta_mle_q12_stdb "Standardized Theta (MLE)" 

***********************************************
* Correcting District code to match with Baseline
***********************************************
  rename district district_or
  recode district_or (2=3) (3=5) (4=7) (5=4) (6=10) (7=8) (8=6) (9=11) (10=2) (11=9), gen(district)
  order district, after(district_or)

***********************************************
* Changes in cluster 387
***********************************************

  replace block = 134 if block==142 & cluster==387
  replace cluster = 462 if cluster ==387

***********************************************
* Saving processed data
***********************************************
  gen last=.
  order prov_id, after(qq0)
  keep case sample3 prov_id-cluster source-last
  order source, first
  drop last
  sort prov_id
  label data "Standardized Patient Pneumonia Second Round"
  compress
  save "$prodata2\standardized_patient_pneumonia_b", replace
